﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Pages
{
    public partial class Account : ClassManager.SessionManagerPage
    {       
        private void Invoke_User(int CompanyID, string Username)
        {
            GPSTrackingDBV2.Entities.Users userEntity = GPSTrackingDBV2.Data.DataRepository.UsersProvider.GetByUsernameCompanyId(Username, CompanyID);

            txtUsername.Text = userEntity.Username;
            txtName.Text = userEntity.Name;
            txtFirstName.Text = userEntity.FirstName;
            txtLastName.Text = userEntity.LastName;
            if (userEntity.Phone != null) txtTelephone.Text = userEntity.Phone.ToString();
            if (userEntity.Mobile != null) txtMobile.Text = userEntity.Mobile.ToString();
            txtEmail.Text = userEntity.Email;

            ViewState["Password"] = userEntity.Password;
            ViewState["IsActive"] = userEntity.IsActive;


        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];         
                Invoke_User(objSessionManager.CompanyID, objSessionManager.Username);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Users userEntity = new GPSTrackingDBV2.Entities.Users();

                userEntity.Username = txtUsername.Text;
                userEntity.OriginalUsername = userEntity.Username;
                userEntity.Password = Convert.ToString(ViewState["Password"]);                
                userEntity.Name = txtName.Text;
                userEntity.FirstName = txtFirstName.Text;
                userEntity.LastName = txtLastName.Text;
                userEntity.CountryId = 0;
                userEntity.StateId = 0;
                userEntity.CityId = 0;
                if (!string.IsNullOrEmpty(txtTelephone.Text)) userEntity.Phone = txtTelephone.Text;
                if (!string.IsNullOrEmpty(txtMobile.Text)) userEntity.Mobile = txtMobile.Text;
                userEntity.Email = txtEmail.Text;

                userEntity.IsActive = Convert.ToBoolean(ViewState["IsActive"]);


                GPSTrackingDBV2.Data.DataRepository.UsersProvider.Update(userEntity);               
            }
            catch (Exception ex)
            {                
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Cuenta error ...');", true);
            }
        }
        
    }
}